package org.darcy.controller.auth;

import java.util.List;

import org.apache.http.HttpStatus;
import org.darcy.entity.privilege.SysMenu;
import org.darcy.entity.privilege.SysUser;
import org.darcy.framework.constant.SysConstant;
import org.darcy.framework.model.R;
import org.darcy.service.privilege.SysMenuService;
import org.darcy.service.privilege.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "登录接口")
@RestController
@RequestMapping("/auth")
@CrossOrigin
@Slf4j
public class LoginController {

	@Autowired
	private SysUserService SysUserService;

	@Autowired
	private SysMenuService SysMenuService;

	@Operation(summary = "用户登录")
	@PostMapping("login")
	public R login(@RequestBody SysUser loginUser) {
		if (StrUtil.isBlank(loginUser.getUsername()) || StrUtil.isBlank(loginUser.getPassword())) {
			return R.error("用户名或密码为空！");
		}
		R data = SysUserService.login(loginUser.getUsername(), loginUser.getPassword());
		return data;
	}

	@Operation(summary = "用户登出")
	@PostMapping("logout")
	public R logout() {
		// 获取当前会话的Session,若无则返空
		SaSession saSession = StpUtil.getSession(false);
		// 若当前session不为空,则从持久库中删除
		if (saSession != null) {
			saSession.logout();
		}
		StpUtil.logout();
		return R.ok("退出成功！");
	}

	@Operation(summary = "获取登录用户")
	@GetMapping("getLoginUser")
	public R getLoginUser() {
		SysUser user = fetchLoginUser();
		if (user != null) {
			return R.ok().data(user);
		} else {
			return R.error(HttpStatus.SC_UNAUTHORIZED, "登录账号过期!");
		}
	}

	private SysUser fetchLoginUser() {
		if (StpUtil.isLogin()) {
			Object loginId = StpUtil.getLoginId();
			SaSession session = StpUtil.getSessionByLoginId(loginId);
			Object userObj = session.get(SysConstant.LOGIN_USER);
			if (userObj != null) {
				return (SysUser) userObj;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Operation(summary = "获取登录用户的菜单列表")
	@GetMapping("getMenu")
	public R getMenu() {
		Object loginId = StpUtil.getLoginId();
		if (loginId == null) {
			log.error("用户没有登录，用户id为空");
			return R.error("用户没有登录，用户id为空");
		}
		SaSession session = StpUtil.getSessionByLoginId(loginId);
		if (session == null) {
			log.error("用户没有登录，用户session为空");
			return R.error("用户没有登录，用户session为空");
		}

		List<SysMenu> menuList = null;
		Object sessionMenuList = session.get(SysConstant.LOGIN_MENU_LIST);
		if (sessionMenuList == null) {
			SysUser loginUser = this.fetchLoginUser();
			menuList = SysMenuService.getMenuList(loginUser.getId());
			session.set(SysConstant.LOGIN_MENU_LIST, menuList);
		} else {
			menuList = (List<SysMenu>) sessionMenuList;
		}
		return R.ok().data(menuList);
	}

}
